package com.example.demo;

import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@SpringBootTest
public class Step {
    @Test
    public void test1() throws InterruptedException {
        long s = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            a(10);
        }
        long e = System.currentTimeMillis();
        System.out.println(e - s);
    }

    public void a(int time){
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public void b(int time)  {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public void c(int time)  {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public void d(int time)  {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test2(){
        long s=System.currentTimeMillis();
        List<CompletableFuture<?>> completableFutures = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            CompletableFuture<Void> future1 = CompletableFuture.runAsync(()->{ a(10); });
            completableFutures.add(future1);
        }

        CompletableFuture<?>[] futures = completableFutures.toArray(new CompletableFuture[0]);
        CompletableFuture<Void> futureAll =CompletableFuture.allOf(futures);
        futureAll.join();
        long e=System.currentTimeMillis();
        System.out.println(e - s);
    }

}
